﻿<script lang="ts" setup name="commodityinformation">
import { ref, reactive, onMounted } from "vue";
import { auth } from '/@/utils/authFunction';
import { ElMessageBox, ElMessage } from "element-plus";
import { downloadStreamFile } from "/@/utils/download";
import { useCommodityinformationApi } from '/@/api/Merchandisemanagement/commodityinformation';
import editDialog from '/@/views/Merchandisemanagement/commodityinformation/component/editDialog.vue'
import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
import ModifyRecord from '/@/components/table/modifyRecord.vue';
import ImportData from "/@/components/table/importData.vue";

const commodityinformationApi = useCommodityinformationApi();
const printDialogRef = ref();
const editDialogRef = ref();
const importDataRef = ref();
const state = reactive({
  exportLoading: false,
  tableLoading: false,
  stores: {},
  showAdvanceQueryUI: false,
  dropdownData: {} as any,
  selectData: [] as any[],
  tableQueryParams: {} as any,
  tableParams: {
    page: 1,
    pageSize: 20,
    total: 0,
    field: 'createTime', // 默认的排序字段
    order: 'descending', // 排序方向
    descStr: 'descending', // 降序排序的关键字符
  },
  tableData: [],
});

// 页面加载时
onMounted(async () => {
  const data = await commodityinformationApi.getDropdownData(true).then(res => res.data.result) ?? {};
  state.dropdownData.goodsID = data.goodsID;
  state.dropdownData.primaryunit = data.primaryunit;
  state.dropdownData.userID = data.userID;
  state.dropdownData.vendorID = data.vendorID;
});

// 查询操作
const handleQuery = async (params: any = {}) => {
  state.tableLoading = true;
  state.tableParams = Object.assign(state.tableParams, params);
  const result = await commodityinformationApi.page(Object.assign(state.tableQueryParams, state.tableParams)).then(res => res.data.result);
  state.tableParams.total = result?.total;
  state.tableData = result?.items ?? [];
  state.tableLoading = false;
};

// 列排序
const sortChange = async (column: any) => {
  state.tableParams.field = column.prop;
  state.tableParams.order = column.order;
  await handleQuery();
};

// 删除
const delCommodityinformation = (row: any) => {
  ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await commodityinformationApi.delete({ id: row.id });
    handleQuery();
    ElMessage.success("删除成功");
  }).catch(() => {});
};

// 批量删除
const batchDelCommodityinformation = () => {
  ElMessageBox.confirm(`确定要删除${state.selectData.length}条记录吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await commodityinformationApi.batchDelete(state.selectData.map(u => ({ id: u.id }) )).then(res => {
      ElMessage.success(`成功批量删除${res.data.result}条记录`);
      handleQuery();
    });
  }).catch(() => {});
};

// 导出数据
const exportCommodityinformationCommand = async (command: string) => {
  try {
    state.exportLoading = true;
    if (command === 'select') {
      const params = Object.assign({}, state.tableQueryParams, state.tableParams, { selectKeyList: state.selectData.map(u => u.id) });
      await commodityinformationApi.exportData(params).then(res => downloadStreamFile(res));
    } else if (command === 'current') {
      const params = Object.assign({}, state.tableQueryParams, state.tableParams);
      await commodityinformationApi.exportData(params).then(res => downloadStreamFile(res));
    } else if (command === 'all') {
      const params = Object.assign({}, state.tableQueryParams, state.tableParams, { page: 1, pageSize: 99999999 });
      await commodityinformationApi.exportData(params).then(res => downloadStreamFile(res));
    }
  } finally {
    state.exportLoading = false;
  }
}

handleQuery();
</script>
<template>
  <div class="commodityinformation-container" v-loading="state.exportLoading">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }"> 
      <el-form :model="state.tableQueryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="state.tableQueryParams.keyword" clearable placeholder="请输入模糊查询关键字"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="货品名称">
              <el-input v-model="state.tableQueryParams.itemName" clearable placeholder="请输入货品名称"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="货品编号">
              <el-input v-model="state.tableQueryParams.itemCode" clearable placeholder="请输入货品编号"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="货品类型">
              <el-select clearable filterable v-model="state.tableQueryParams.goodsID" placeholder="请选择货品类型">
                <el-option v-for="(item,index) in state.dropdownData.goodsID ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="规格型号">
              <el-input v-model="state.tableQueryParams.specifications" clearable placeholder="请输入规格型号"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="入库参考价/元">
              <el-input-number v-model="state.tableQueryParams.enterPrice"  clearable placeholder="请输入入库参考价/元"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="出库参考价/元">
              <el-input-number v-model="state.tableQueryParams.gooutPrice"  clearable placeholder="请输入出库参考价/元"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="显示顺序">
              <el-input-number v-model="state.tableQueryParams.displayOrder"  clearable placeholder="请输入显示顺序"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="状态">
              <el-input v-model="state.tableQueryParams.state" clearable placeholder="请输入状态"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="保质期管理">
              <el-input v-model="state.tableQueryParams.shelfManagement" clearable placeholder="请输入保质期管理"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="保质期">
              <el-input-number v-model="state.tableQueryParams.shelflife"  clearable placeholder="请输入保质期"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="保质期单位">
              <el-input v-model="state.tableQueryParams.shelfUnits" clearable placeholder="请输入保质期单位"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="预警天数（天）">
              <el-input-number v-model="state.tableQueryParams.earlywarning"  clearable placeholder="请输入预警天数（天）"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="备注">
              <el-input v-model="state.tableQueryParams.remark" clearable placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="启用多单位">
              <el-input v-model="state.tableQueryParams.multiUnitEnabled" clearable placeholder="请输入启用多单位"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="主单位">
              <el-select clearable filterable v-model="state.tableQueryParams.primaryunit" placeholder="请选择主单位">
                <el-option v-for="(item,index) in state.dropdownData.primaryunit ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="条码">
              <el-input v-model="state.tableQueryParams.primaryunitBarcode" clearable placeholder="请输入条码"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="辅助单位">
              <el-input v-model="state.tableQueryParams.auxiliaryUnits" clearable placeholder="请输入辅助单位"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="库存信息">
              <el-input v-model="state.tableQueryParams.inventoryList" clearable placeholder="请输入库存信息"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="上传图片">
              <el-input v-model="state.tableQueryParams.uploadImage" clearable placeholder="请输入上传图片"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="上传附件">
              <el-input v-model="state.tableQueryParams.uploadAttachments" clearable placeholder="请输入上传附件"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="客户名称">
              <el-select clearable filterable v-model="state.tableQueryParams.userID" placeholder="请选择客户名称">
                <el-option v-for="(item,index) in state.dropdownData.userID ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="state.showAdvanceQueryUI">
            <el-form-item label="供应商">
              <el-select clearable filterable v-model="state.tableQueryParams.vendorID" placeholder="请选择供应商">
                <el-option v-for="(item,index) in state.dropdownData.vendorID ?? []" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item >
              <el-button-group style="display: flex; align-items: center;">
                <el-button type="primary"  icon="ele-Search" @click="handleQuery" v-auth="'commodityinformation:page'" v-reclick="1000"> 查询 </el-button>
                <el-button icon="ele-Refresh" @click="() => state.tableQueryParams = {}"> 重置 </el-button>
                <el-button icon="ele-ZoomIn" @click="() => state.showAdvanceQueryUI = true" v-if="!state.showAdvanceQueryUI" style="margin-left:5px;"> 高级查询 </el-button>
                <el-button icon="ele-ZoomOut" @click="() => state.showAdvanceQueryUI = false" v-if="state.showAdvanceQueryUI" style="margin-left:5px;"> 隐藏 </el-button>
                <el-button type="danger" style="margin-left:5px;" icon="ele-Delete" @click="batchDelCommodityinformation" :disabled="state.selectData.length == 0" v-auth="'commodityinformation:batchDelete'"> 删除 </el-button>
                <el-button type="primary" style="margin-left:5px;" icon="ele-Plus" @click="editDialogRef.openDialog(null, '新增货品信息')" v-auth="'commodityinformation:add'"> 新增 </el-button>
                <el-dropdown :show-timeout="70" :hide-timeout="50" @command="exportCommodityinformationCommand">
                  <el-button type="primary" style="margin-left:5px;" icon="ele-FolderOpened" v-reclick="20000" v-auth="'commodityinformation:export'"> 导出 </el-button>
                  <template #dropdown>
                    <el-dropdown-menu>
                      <el-dropdown-item command="select" :disabled="state.selectData.length == 0">导出选中</el-dropdown-item>
                      <el-dropdown-item command="current">导出本页</el-dropdown-item>
                      <el-dropdown-item command="all">导出全部</el-dropdown-item>
                    </el-dropdown-menu>
                  </template>
                </el-dropdown>
                <el-button type="warning" style="margin-left:5px;" icon="ele-MostlyCloudy" @click="importDataRef.openDialog()" v-auth="'commodityinformation:import'"> 导入 </el-button>
              </el-button-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 5px">
      <el-table :data="state.tableData" @selection-change="(val: any[]) => { state.selectData = val; }" style="width: 100%" v-loading="state.tableLoading" tooltip-effect="light" row-key="id" @sort-change="sortChange" border>
        <el-table-column type="selection" width="40" align="center" v-if="auth('commodityinformation:batchDelete') || auth('commodityinformation:export')" />
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop='itemName' label='货品名称' show-overflow-tooltip />
        <el-table-column prop='itemCode' label='货品编号' show-overflow-tooltip />
        <el-table-column prop='goodsID' label='货品类型' :formatter="(row: any) => row.goodsFkDisplayName" show-overflow-tooltip />
        <el-table-column prop='specifications' label='规格型号' show-overflow-tooltip />
        <el-table-column prop='enterPrice' label='入库参考价/元' show-overflow-tooltip />
        <el-table-column prop='gooutPrice' label='出库参考价/元' show-overflow-tooltip />
        <el-table-column prop='displayOrder' label='显示顺序' show-overflow-tooltip />
        <el-table-column prop='state' label='状态' show-overflow-tooltip>
          <template #default="scope">
            <el-tag v-if="scope.row.state"> 是 </el-tag>
            <el-tag type="danger" v-else> 否 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop='shelfManagement' label='保质期管理' show-overflow-tooltip>
          <template #default="scope">
            <el-tag v-if="scope.row.shelfManagement"> 是 </el-tag>
            <el-tag type="danger" v-else> 否 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop='shelflife' label='保质期' show-overflow-tooltip />
        <el-table-column prop='shelfUnits' label='保质期单位' show-overflow-tooltip />
        <el-table-column prop='earlywarning' label='预警天数（天）' show-overflow-tooltip />
        <el-table-column prop='remark' label='备注' show-overflow-tooltip />
        <el-table-column prop='multiUnitEnabled' label='启用多单位' show-overflow-tooltip>
          <template #default="scope">
            <el-tag v-if="scope.row.multiUnitEnabled"> 是 </el-tag>
            <el-tag type="danger" v-else> 否 </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop='primaryunit' label='主单位' :formatter="(row: any) => row.primaryunitFkDisplayName" show-overflow-tooltip />
        <el-table-column prop='primaryunitBarcode' label='条码' show-overflow-tooltip />
        <el-table-column prop='auxiliaryUnits' label='辅助单位' show-overflow-tooltip />
        <el-table-column prop='inventoryList' label='库存信息' show-overflow-tooltip />
        <el-table-column prop='uploadImage' label='上传图片' show-overflow-tooltip />
        <el-table-column prop='uploadAttachments' label='上传附件' show-overflow-tooltip />
        <el-table-column prop='userID' label='客户名称' :formatter="(row: any) => row.userFkDisplayName" show-overflow-tooltip />
        <el-table-column prop='vendorID' label='供应商' :formatter="(row: any) => row.vendorFkDisplayName" show-overflow-tooltip />
        <el-table-column label="修改记录" width="100" align="center" show-overflow-tooltip>
          <template #default="scope">
            <ModifyRecord :data="scope.row" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip v-if="auth('commodityinformation:update') || auth('commodityinformation:delete')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text type="primary" @click="editDialogRef.openDialog(scope.row, '编辑货品信息')" v-auth="'commodityinformation:update'"> 编辑 </el-button>
            <el-button icon="ele-Delete" size="small" text type="primary" @click="delCommodityinformation(scope.row)" v-auth="'commodityinformation:delete'"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination 
              v-model:currentPage="state.tableParams.page"
              v-model:page-size="state.tableParams.pageSize"
              @size-change="(val: any) => handleQuery({ pageSize: val })"
              @current-change="(val: any) => handleQuery({ page: val })"
              layout="total, sizes, prev, pager, next, jumper"
              :page-sizes="[10, 20, 50, 100, 200, 500]"
              :total="state.tableParams.total"
              size="small"
              background />
      <ImportData ref="importDataRef" :import="commodityinformationApi.importData" :download="commodityinformationApi.downloadTemplate" v-auth="'commodityinformation:import'" @refresh="handleQuery"/>
      <printDialog ref="printDialogRef" :title="'打印货品信息'" @reloadTable="handleQuery" />
      <editDialog ref="editDialogRef" @reloadTable="handleQuery" />
    </el-card>
  </div>
</template>
<style scoped>
:deep(.el-input), :deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>